// pages/search/product/orderDetail/orderDetail.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    m:0,
    s:0,
    bgImg:'http://delice.riki.cn/mavista/img/delivery-state-bg.png',
    img1:'http://delice.riki.cn/mavista/img/baby_barash.png',
    imgs:[
      "http://delice.riki.cn/mavista/img/collection-icon.png",
      'http://delice.riki.cn/mavista/img/pioneer.png',
      'http://delice.riki.cn/mavista/img/explorer.png',
      'http://delice.riki.cn/mavista/img/adventurer.png',
      'http://delice.riki.cn/mavista/img/expedition Leader.png'
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

    let m, s
    let _this = this;

    let authCode = { 'authCode': wx.getStorageSync('authCode') };
    app.requestData('/api/purchase/order/unpaid', authCode, 'POST', function (res) {
      console.log('-----',res);
      //================================ 返回的参数有问题 wechatPay=>   package  =>  prepay_id=null  ======================
      let discountValue = res.discountValue.toFixed(2)
      let price = (res.totalAmount - res.discountValue).toFixed(2)
      
      _this.setData({
        order: res,
        discountValue: discountValue,
        price:price
      })
     
      let date = parseInt(900 - (new Date().valueOf() - res.created) / 1000);
      let time = setInterval(function () {
        if (date < 0) {
          console.log('111')
          s = 0;
          m = 0;
          clearInterval(time)
          wx.navigateTo({
            url: '../product',
          })
        } else {
          // console.log('222')
          date--;
          s = date % 60;
          m = parseInt(date / 60);
        }

        _this.setData({
          m: m,
          s: s
        })
      }, 1000)
    }, function (res) {
      console.log(res)
    })

    // this.setData({
    //   // index:options.index-1
    //   index: 1
    // })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  
  wxPay(){
    let order = this.data.order;
    let timeStamp = String(order.wechatPay.timeStamp); 
    if (order.purchaseApp == 'NORMAL') {
      wx.showToast({
        image:'../../../../images/gantanhao.png',
        title: '请去公众号支付',
      })
      return
    }
     wx.requestPayment({
       timeStamp: timeStamp,
       nonceStr: order.wechatPay.nonceStr,
       package: order.wechatPay.package,
       signType: 'MD5',
       paySign: order.wechatPay.paySign,
       success(res){
         console.log('支付',res)
            wx.switchTab({
              url: '../../../index/index',
            })
          }
      })
  }
})